package com.jokerx.ohos.fasttrpg.play.slice;

import com.jokerx.ohos.fasttrpg.MainAbility;
import com.jokerx.ohos.fasttrpg.ResourceTable;
import com.jokerx.ohos.fasttrpg.gamemaster.GameMasterServiceRemoteProxy;
import com.jokerx.ohos.fasttrpg.play.PlayAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.ability.continuation.ExtraParams;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.RadioContainer;
import ohos.bundle.ElementName;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;

public class SelectRoleAbilitySlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, PlayAbility.class.getName());
    private int selectedRole;
    private String localDeviceId;
    private String masterDeviceId;

    private GameMasterServiceRemoteProxy gameMasterServiceRemoteProxy;

    // 创建连接GameMasterService回调实例
    private IAbilityConnection connection = new IAbilityConnection() {
        // 连接到Service的回调
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
            HiLog.info(TAG, "elementName=%{public}s, iRemoteObject=%{public}s, resultCode=%{public}s", elementName, iRemoteObject,
                    resultCode);
            // Client侧需要定义与Service侧相同的IRemoteObject实现类。开发者获取服务端传过来IRemoteObject对象，并从中解析出服务端传过来的信息。
            gameMasterServiceRemoteProxy = new GameMasterServiceRemoteProxy(iRemoteObject);
        }

        // Service异常死亡的回调
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_slice_select_role);
        masterDeviceId = intent.getStringParam("masterDeviceId");
        localDeviceId = intent.getOperation().getDeviceId();
        initView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initView() {
        RadioContainer roleSelector = (RadioContainer) findComponentById(ResourceTable.Id_role_selector);
        roleSelector.setMarkChangedListener((radioContainer, i) -> {
            selectedRole = i;
        });
        Button btnConfirmRole= (Button) findComponentById(ResourceTable.Id_btn_confirm_role);
        btnConfirmRole.setClickedListener(component -> {
            // 连接Service
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId(masterDeviceId)
                    .withBundleName(getBundleName())
                    .withAbilityName("com.jokerx.ohos.fasttrpg.gamemaster.GameMasterServiceAbility")
                    .build();
            intent.setOperation(operation);
            boolean connectAbilityFlag = connectAbility(intent, connection);
            if (connectAbilityFlag) {
                gameMasterServiceRemoteProxy.selectRole(localDeviceId, selectedRole);
                routeWaitPage();
            }
        });
    }

    private void routeWaitPage() {
        present(new WaitingAbilitySlice(), new Intent());
    }
}
